package cn.ctodb.mall.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A Member.
 */
@Entity
@Table(name = "member")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "member")
public class Member implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "usercode")
    private String usercode;

    @Column(name = "birthday")
    private ZonedDateTime birthday;

    @OneToOne
    @JoinColumn(unique = true)
    private MemberAddress defaultAddress;

    @OneToMany(mappedBy = "member")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<MemberAddress> addresses = new HashSet<>();

    @ManyToOne
    private Shop shop;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsercode() {
        return usercode;
    }

    public Member usercode(String usercode) {
        this.usercode = usercode;
        return this;
    }

    public void setUsercode(String usercode) {
        this.usercode = usercode;
    }

    public ZonedDateTime getBirthday() {
        return birthday;
    }

    public Member birthday(ZonedDateTime birthday) {
        this.birthday = birthday;
        return this;
    }

    public void setBirthday(ZonedDateTime birthday) {
        this.birthday = birthday;
    }

    public MemberAddress getDefaultAddress() {
        return defaultAddress;
    }

    public Member defaultAddress(MemberAddress memberAddress) {
        this.defaultAddress = memberAddress;
        return this;
    }

    public void setDefaultAddress(MemberAddress memberAddress) {
        this.defaultAddress = memberAddress;
    }

    public Set<MemberAddress> getAddresses() {
        return addresses;
    }

    public Member addresses(Set<MemberAddress> memberAddresses) {
        this.addresses = memberAddresses;
        return this;
    }

    public Member addAddress(MemberAddress memberAddress) {
        addresses.add(memberAddress);
        memberAddress.setMember(this);
        return this;
    }

    public Member removeAddress(MemberAddress memberAddress) {
        addresses.remove(memberAddress);
        memberAddress.setMember(null);
        return this;
    }

    public void setAddresses(Set<MemberAddress> memberAddresses) {
        this.addresses = memberAddresses;
    }

    public Shop getShop() {
        return shop;
    }

    public Member shop(Shop shop) {
        this.shop = shop;
        return this;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Member member = (Member) o;
        if (member.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, member.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "Member{" +
            "id=" + id +
            ", usercode='" + usercode + "'" +
            ", birthday='" + birthday + "'" +
            '}';
    }
}
